package com.example.owner.factory;

import com.example.owner.annotation.Cylinders;
import com.example.owner.bean.Engine;
import com.example.owner.bean.impl.V11Engine;
import com.example.owner.bean.impl.V12Engine;
import com.example.owner.bean.impl.V6Engine;
import com.example.owner.bean.impl.V8Engine;
import com.example.owner.conf.EngineConfiguration;
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.EachBean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Prototype;
import io.micronaut.context.exceptions.DisabledBeanException;
import io.micronaut.inject.InjectionPoint;
import jakarta.inject.Singleton;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Factory
public class EngineFactory {

//    @EachBean(EngineConfiguration.class)
//    public Engine buildEngine(EngineConfiguration engineConfiguration) {
//        log.info("Building engine with configuration {}", engineConfiguration);
//        if (engineConfiguration.isEnabled()) {
//            return new V11Engine();
//        } else {
//            throw new DisabledBeanException("Engine configuration disabled");
//        }
//    }
//
//
//    @Singleton
//    public Engine buildEngine1() {
//        log.info("Building engine with buildEngine1Ï ");
//        return new V12Engine();
//    }


    @Prototype
    Engine v11Engine(InjectionPoint<?> injectionPoint) { // (1)
        Class<? extends InjectionPoint> aClass = injectionPoint.getClass();


        final int cylinders = injectionPoint
                .getAnnotationMetadata()
                .intValue(Cylinders.class).orElse(11); // (2)

        log.info("cylinders v11Engine: {} {}", aClass, cylinders);
        switch (cylinders) { // (3)
            case 11:
                return new V11Engine();
            case 12:
                return new V12Engine();
            default:
                throw new IllegalArgumentException("Unsupported number of cylinders specified: " + cylinders);
        }
    }
}
